package homework3;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageFileSplitter {
    public static void splitImageBySize(File sourceImage, int chunkSize) throws IOException {
        // 读取源图片文件
        FileInputStream inputStream = new FileInputStream(sourceImage);
        byte[] buffer = new byte[chunkSize];
        
        int bytesRead;
        int count = 0;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            // 创建新的切割文件
            File chunkFile = new File(sourceImage.getParent(), sourceImage.getName() + "-" + count);
            FileOutputStream outputStream = new FileOutputStream(chunkFile);
            
            // 将切割的数据写入新的文件
            outputStream.write(buffer, 0, bytesRead);
            
            outputStream.close();
            count++;
        }
        
        inputStream.close();
    }
    
    public static void main(String[] args) {
        try {
            File sourceImage = new File("path/to/source/image.jpg");
            int chunkSize = 10 * 1024 * 1024; // 10MB
            
            splitImageBySize(sourceImage, chunkSize);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}